package cn.ppwx.crm;

import java.util.Date;
import java.util.List;

import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.ppwx.crm.common.log.CrmLogger;
import cn.ppwx.crm.modules.sys.entity.Leave;
import cn.ppwx.crm.modules.sys.entity.User;
import cn.ppwx.crm.modules.sys.listener.SessionCache;
import cn.ppwx.crm.modules.sys.service.LeaveService;
import cn.ppwx.crm.modules.sys.service.UserService;
import cn.ppwx.crm.modules.sys.service.WorkFlowService;

/**
 * Created by peter.pi on 16/11/25.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring-context.xml" ,"classpath:spring-context-activiti.xml" })  
public class testLeave {


	@Autowired
	private  WorkFlowService workFlowService;
	
	
	@Autowired
	private LeaveService leaveService;
	
	
	@Autowired
	private UserService userService;
	

	
	/**
	 * 部署一个流程定义，实际开发中，可以做一个按zip自定义的部署 
	 */
	@Test
	public void testDep(){
		
		workFlowService.deploymentProcessDefinition();
		
	}

	
	
	/**
	 * 先红启动一个流程
	 */
	@Test
	public  void testApplyLeave(){
		
		
		User  user=new User();
		user.setUserId(4L);
		user.setUserName("先红");
		user.setManagerId(3L);
		SessionCache.loginUser.put("user", user);
		
		
		Leave leave = new Leave();
		
		leave.setStartTime(new Date());
		leave.setEndTime(new Date());
		
		leave.setUserId(4);
		
		leaveService.createLeave(leave);
		
	}
	
	/**
	 * 先红查询当前的流程任务
	 */
	@Test
	public  void testQueryToDoList(){
		
		User  user = new User();
		user.setUserId(4L);
		user.setUserName("先红");
		
		
		List<Task> tasks = userService.queryTodoTask(user);
		
		if(tasks!=null&&tasks.size()>0){
			
			CrmLogger.log.info(tasks);
			
		}
	}
	
	
	/**
	 * 先红提交流程
	 */
	@Test
	public void testCheckApply_1(){
		
	
		User  user=new User();
		user.setUserId(4L);
		user.setManagerId(3L);
		user.setUserName("先红");
		SessionCache.loginUser.put("user", user);
		
		Task task= userService.queryTodoTask(user).get(0);
		
		CrmLogger.log.info(task.getId()+" "+task.getAssignee());
		
		workFlowService.completeTask(task.getId(),"想休息");
		
		
	}
	
	

	/**
	 * 贸海逐级审批或驳回
	 */
	@Test
	public void testCheckApply(){
		
		String approveResult="true";
		
		User  user=new User();
		user.setUserId(3L);
		user.setManagerId(2L);
		user.setUserName("吴贸海");
		SessionCache.loginUser.put("user", user);
		Task task= userService.queryTodoTask(user).get(0);
		
		CrmLogger.log.info(task.getId()+" "+task.getAssignee());
		leaveService.manageApprove(task.getId(), user, approveResult,"表现优秀,同意");
		
		
	}



	/**
	 * LANY逐级审批或驳回
	 */
	@Test
	public void testCheckApply_2(){
		
		String approveResult="true";
		
		User  user=new User();
		user.setUserId(2L);
		user.setManagerId(1L);
		user.setUserName("lany");
		SessionCache.loginUser.put("user", user);
		Task task= userService.queryTodoTask(user).get(0);
		
		CrmLogger.log.info(task.getId()+" "+task.getAssignee());
		leaveService.manageApprove(task.getId(), user, approveResult,"表现优秀,同意");
		
		
	}



	/**
	 * cio逐级审批或驳回
	 */
	@Test
	public void testCheckApply_3(){
		
		String approveResult="true";
		
		User  user=new User();
		user.setUserId(1L);
		user.setManagerId(0L);
		user.setUserName("cio");
		SessionCache.loginUser.put("user", user);
		Task task= userService.queryTodoTask(user).get(0);
		
		CrmLogger.log.info(task.getId()+" "+task.getAssignee());
		leaveService.manageApprove(task.getId(), user, approveResult,"同意");
		
		
	}
	
	
	/**
	 * hr备案
	 */
	@Test
	public void testCheckApply_4(){
		User  user=new User();
		user.setUserId(7L);
		user.setManagerId(0L);
		user.setUserName("静静");
		SessionCache.loginUser.put("user", user);
		Task task= userService.queryTodoTask(user).get(0);
		
		CrmLogger.log.info(task.getId()+" "+task.getAssignee());
		
		workFlowService.completeTask(task.getId(),"扣全奖");
	}


}
